from fullsteam import socket
from fullsteam import game
from fullsteam import json
from fullsteam import mapgen


def public(func):
    func.public = True
    return func


class Input(socket.SteamSocket, json.JSONable):
    _json_attrs = ('upside_down', 'username', 'position')

    def open(self):
        print "INPUT WebSocket opened"
        self.auth = False
        self.upside_down = False
        self.position = None

    def on_json(self, message):
        if not 'method' in message or not 'arguments' in message:
            print 'INPUT not parsable call', message
            return

        method = getattr(self, message['method'])
        if not hasattr(method, 'public') or not method.public:
            print 'CALL TO NOT PUBLIC FUNCTION', message['method']
            return

        method(**message['arguments'])

    def on_close(self):
        print "INPUT WebSocket closed"
        game.GAME.lost_input(self)

    @public
    def is_upside_down(self, is_upside_down):
        self.upside_down = is_upside_down

    @public
    def shake_event(self):
        if self.position == 'shake':
            print 'wooho shake it ', self.username, '!'
            for line in game.GAME._lines:
                if isinstance(line, mapgen.Log):
                    line.health -= 1
                    print 'h', line.health

                    if line.health <= 0:
                        if line.content[mapgen.MIDDLE] != None:
                            line.content[mapgen.MIDDLE] = None
                            game.write_viewers({"remove_middle": line.id})
                            game.write_viewers({"message": 'Log DONE', 'duration': 3000})
                    else:
                        game.write_viewers({"message": 'Log ' + str(line.health), 'duration': 3000})
                    break

    @public
    def swipe_event(self, direction):
        direction = direction.replace('swipe', '')
        print 'swipe', repr(direction)
        if self.position == 'swipe':
            print 'swipte to', direction, ' by ', self.username
            for line in game.GAME._lines:
                if isinstance(line, mapgen.Turn):
                    if direction == line.done:
                        line.health -= 1
                    else:
                        line.health += 1
                    print 'h', line.health
                    if line.health <= 0:
                        game.write_viewers({"next_direction": line.done}) # XXX
                        game.write_viewers({"message": 'Crossing DONE' + str(line.health), 'duration': 3000})
                    else:
                        game.write_viewers({"message": 'Crossing ' + str(line.health), 'duration': 3000})
                        game.write_viewers({"next_direction": line.fail}) # XXX
                    break

    @public
    def shovel_event(self):
        print 'shovel ', self.username
        if self.position == 'shovel':
            game.GAME.coal_state += 15
            if game.GAME.coal_state > 300:
                game.GAME.coal_state = 300
            print 'NEW coal state', game.GAME.coal_state

    @public
    def login(self, username, version):
        if version != '0.1':
            self.call('on_error', "Wrong version.")
        elif len(username) <= 3 or username.lower() == 'penis':
            self.call('on_error', "Username to short.")
        else:
            self.auth = True
            self.is_ready = False
            self.username = username
            self.mode = 'shake'
            self.looks_down = False
            game.GAME.new_input(self)
            self.call('logged_in' )

    @public
    def ready(self):
        self.call('start_game', 'flip', True) # we already want to know where up is
        self.is_ready = True
        game.GAME.check_go()

